﻿using Nancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using UserService.Business;
using UserService.Entity;
using JWT;

namespace jczUserService.Modules
{
    public class ClientModule : NancyModule
    {
        public ClientModule() : base("/client")
        {
            Before += ctx => {
                string sjwt = Request.Headers.Authorization.ToString(); //this.Context.Request.Session["UserToken"] == null ? "": this.Context.Request.Session["UserToken"].ToString();
               // string SecretKey = "9fdb64ec22e9cda94ad9964d07dd9dd16ee8c41a8e0d4367e1d09d2520ae583cb8357a992edb76a74a4b866243580b6106f6497733641f588f8608f391443138";
                string _RootPath = AppDomain.CurrentDomain.GetData(".appPath").ToString();
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = _RootPath + @"\bin\jczUserService.dll.config";
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                string SecretKey = config.AppSettings.Settings["SecretKey"].Value;
                string RandomKey = config.AppSettings.Settings["RandomKey"].Value;
                byte[] byteArray = System.Text.Encoding.Default.GetBytes(SecretKey);
                bool tokenVerification = JsonWebToken.VerificationToken(sjwt, byteArray, RandomKey);
                if (!tokenVerification)
                {
                    var revalue = new
                    {
                        code = "401",
                        reMessage = "/login"
                    };
                    return Response.AsJson(revalue);
                }
                    
                System.Diagnostics.Debug.WriteLine("indexModule---Before-------------" + sjwt);
                return null;
            };
            Get["/{genre}"] = _ =>
            {
                string genre = _.genre;//wz_client
                Client obj = null;
                using (ClientBusiness _repo = new ClientBusiness())
                {
                    obj = _repo.FindClient(genre);
                }
                //return "person name :" + genre;
                var revalue = new
                {
                    code = "200",
                    reMessage = obj
                };
                return Response.AsJson(revalue);
            };

            Get["/Add"] = _ =>
            {
                bool b_add = false;
                using(ClientBusiness _repo = new ClientBusiness())
                {
                    //b_add = _repo.InstertClient();
                }
                if (b_add) return Response.AsText("成功", "text/html;charset=UTF-8");
                return Response.AsText("失败", "text/html;charset=UTF-8");
            };

            Get["/authfronturl"] = _ =>
            {
                return Response.AsRedirect("http://localhost:8082");
            };

            Get["/authbackurl"] = _ =>
            {
                return Response.AsRedirect("http://localhost:8082");
            };
        }
    }
}
